package com.shuoyue.ycgk.ui.common;

import android.util.Log;

import com.shuoyue.appdepends.commonutils.XLog;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.fileupload.FileUploadObserver;
import com.shuoyue.ycgk.net.base.fileupload.UploadFileRequestBody;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;

public interface UploadFileContract {
    interface View extends BaseView {
        void onUploadProgress(int progress, String... TAG);

        void onUploadSuc(String serverPath, String... TAG);

        void onUploadFailed(String... TAG);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void uploadFile(File file, String tipsFileName, boolean isShowDialog, String... TAG) {
            FileUploadObserver<Optional<String>> fileUploadObserver =
                    new FileUploadObserver<Optional<String>>(mView, this, isShowDialog) {

                        @Override
                        public void onUpLoadSuccess(Optional<String> responseBody) {
                            Log.e("progress", "onUpLoadSuccess: " + Thread.currentThread().getId());
                            mView.toast(tipsFileName + "上传成功");
                            XLog.e("upload", tipsFileName + "上传成功");
                            mView.onUploadSuc(responseBody.getIncludeNull(), TAG);
                        }

                        @Override
                        public void onUpLoadFail(Throwable e) {
                            Log.e("progress", "onUpLoadFail: " + Thread.currentThread().getId());

                            mView.toast(tipsFileName + "上传失败");
                            XLog.e("upload", tipsFileName + "上传失败");

                            mView.onUploadFailed(TAG);
                        }

                        @Override
                        public void onProgress(int progress) {
                            Log.e("progress", "onProgress: " + Thread.currentThread().getId());
                            XLog.e("upload", "progress:" + progress);
                            mView.onUploadProgress(progress, TAG);
                        }
                    };
            UploadFileRequestBody uploadFileRequestBody =
                    new UploadFileRequestBody(file, fileUploadObserver);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), uploadFileRequestBody);
            apply(model.upload(part)).subscribe(fileUploadObserver);
        }

        public void uploadFile(File file, String tipsFileName, String... TAG) {
            uploadFile(file, tipsFileName, true, TAG);
        }
    }

    class Model {
        public Observable<BaseResult<String>> upload(MultipartBody.Part file) {
            return RetrofitClient.getInstance().getApi()
                    .uploadFile(file)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    }

}
